package com.link2room.rodimus.service.impl;

import com.link2room.common.constant.DefaultValue;
import com.link2room.common.dao.ICommonDao;
import com.link2room.common.jpa.criteria.CriteriaUtil;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.rodimus.constant.ProcedureConstant;
import com.link2room.rodimus.entity.MemberBase;
import com.link2room.rodimus.service.IMemberService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by LN on 2017/4/19.
 */
@Component("memberService")
public class MemberServiceImpl implements IMemberService {
    @Resource
    private ICommonDao commonDao;

    @Override
    public MemberBase selectMemberByMobile(String lhotelGroupCode, String mobile) throws Exception {
        MemberBase find = new MemberBase();
        find.setMobile(mobile);
        find.setLhotelGroupCode(lhotelGroupCode);
        find.setIsHalt(DefaultValue.F);
        return commonDao.findOne(MemberBase.class, CriteriaUtil.getCriteriaAllEQ(find));
    }

    @Override
    public boolean hasMemberByMobile(String lhotelGroupCode, String mobile) throws Exception {
        MemberBase find = new MemberBase();
        find.setLhotelGroupCode(lhotelGroupCode);
        find.setMobile(mobile);
        find.setIsHalt(DefaultValue.F);
        long count = commonDao.getCount(MemberBase.class, CriteriaUtil.getCriteriaAllEQ(find), null);
        return count > 0;
    }

    private final Lock memberNoLock = new ReentrantLock();

    @Override
    public String syncGetNewMemberNo() throws Exception {
        String newMemberNo = "";
        memberNoLock.tryLock(2, TimeUnit.SECONDS);
        try {
            String procedureName = ProcedureConstant._createNo.procedureName;
            List params = new ArrayList();
            params.add(ProcedureConstant._createNo._params.MEMBER);
            String querySQL = "{call " + procedureName + "()}";
            if (null != params) {
                querySQL = "{call " + procedureName + "(" + "?" + ")}";
            } else {
                querySQL = "{call " + procedureName + "()}";
            }

            QueryResult<String> noResult = commonDao.findByNativeOneField(querySQL, null, params, null, null, null);
            newMemberNo = String.valueOf(noResult.getResultlist().get(0));
            return newMemberNo;
        } finally {
            memberNoLock.unlock();
        }
    }

    @Override
    public MemberBase selectMemberByOpenid(String lhotelGroupCode, String openid) throws Exception {
        MemberBase find = new MemberBase();
        find.setLhotelGroupCode(lhotelGroupCode);
        find.setOpenid(openid);
        find.setIsHalt(DefaultValue.F);
        return commonDao.findOne(MemberBase.class,CriteriaUtil.getCriteriaAllEQ(find));
    }

    @Override
    public MemberBase selectMemberBaseByNo(String lhotelGroupCode, String memberNo) throws Exception {
        MemberBase find = new MemberBase();
        find.setLhotelGroupCode(lhotelGroupCode);
        find.setMemberNo(memberNo);
        return commonDao.findOne(MemberBase.class,CriteriaUtil.getCriteriaAllEQ(find));
    }
}
